package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.nhc.model.LambertParmFileHandle;
import com.nhc.model.LambertSetInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.TopMenuWindow;

public class LambertSetActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "LambertSetActivity";

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */

    public static Intent createIntent(Context context) {
        return new Intent(context, LambertSetActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    LambertSetInfo mLambertSetInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lambertset_activity);

        mLambertSetInfo = LambertParmFileHandle.getInstance().mLambertSetInfoEdit;
        if (mLambertSetInfo == null) {
            finish();
            return;
        }

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_ZLAT;
    private TextView tv_ZLON;
    private TextView tv_parallel1;
    private TextView tv_parallel2;
    private TextView tv_TY;
    private TextView tv_utmc;
    private TextView tv_EW;
    private TextView tv_SN;


    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);

        tv_ZLAT = (TextView) findViewById(R.id.tv_ZLAT);
        tv_ZLON = (TextView) findViewById(R.id.tv_ZLON);
        tv_parallel1 = (TextView) findViewById(R.id.tv_parallel1);
        tv_parallel2 = (TextView) findViewById(R.id.tv_parallel2);
        tv_TY = (TextView) findViewById(R.id.tv_TY);
        tv_utmc = (TextView) findViewById(R.id.tv_utmc);
        tv_EW = (TextView) findViewById(R.id.tv_EW);
        tv_SN = (TextView) findViewById(R.id.tv_SN);

        if (LambertParmFileHandle.getInstance().lambert0 == mLambertSetInfo) {
            findViewById(R.id.lambertset_parallel2).setVisibility(View.GONE);
        }

        String[] S_TITLES = getResources().getStringArray(R.array.topbar1_names_7);
        String[] L_TITLES = getResources().getStringArray(R.array.topbar1_names_6);
        if (mLambertSetInfo == LambertParmFileHandle.getInstance().stereograph0) {
            tvBaseTitle.setText(S_TITLES[1] + getString(R.string.cssz));
        } else if (mLambertSetInfo == LambertParmFileHandle.getInstance().stereograph1) {
            tvBaseTitle.setText(S_TITLES[2] + getString(R.string.cssz));
        } else if (mLambertSetInfo == LambertParmFileHandle.getInstance().stereograph2) {
            tvBaseTitle.setText(S_TITLES[3] + getString(R.string.cssz));
        } else if (mLambertSetInfo == LambertParmFileHandle.getInstance().stereograph3) {
            tvBaseTitle.setText(S_TITLES[4] + getString(R.string.cssz));
        } else if (mLambertSetInfo == LambertParmFileHandle.getInstance().lambert0) {
            tvBaseTitle.setText(L_TITLES[2] + getString(R.string.setting));
        } else if (mLambertSetInfo == LambertParmFileHandle.getInstance().lambert1) {
            tvBaseTitle.setText(L_TITLES[1] + getString(R.string.setting));
        } else if (mLambertSetInfo == LambertParmFileHandle.getInstance().polyconic) {
            tvBaseTitle.setText(R.string.dyztysz);
        }
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void initData() {//必须调用
        refresh();
    }

    void refresh() {
        tv_ZLAT.setText(mLambertSetInfo.ZLAT + "");
        tv_ZLON.setText(mLambertSetInfo.ZLON + "");
        tv_TY.setText(mLambertSetInfo.TY + "");
        tv_EW.setText(mLambertSetInfo.EW + "");
        tv_SN.setText(mLambertSetInfo.SN + "");

        tv_parallel1.setText(mLambertSetInfo.parallel1 + "");
        tv_parallel2.setText(mLambertSetInfo.parallel2 + "");
        tv_utmc.setText(mLambertSetInfo.utmc + "");
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.lambertset_0).setOnClickListener(this);
        findViewById(R.id.lambertset_1).setOnClickListener(this);
        findViewById(R.id.lambertset_2).setOnClickListener(this);
        findViewById(R.id.lambertset_3).setOnClickListener(this);
        findViewById(R.id.lambertset_4).setOnClickListener(this);

        findViewById(R.id.lambertset_parallel1).setOnClickListener(this);
        findViewById(R.id.lambertset_parallel2).setOnClickListener(this);
        findViewById(R.id.lambertset_utmc).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        LambertParmFileHandle.getInstance().save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lambertset_0:
                editParm(0);
                break;
            case R.id.lambertset_1:
                editParm(1);
                break;
            case R.id.lambertset_2:
                editParm(2);
                break;
            case R.id.lambertset_3:
                editParm(3);
                break;
            case R.id.lambertset_4:
                editParm(4);
                break;
            case R.id.lambertset_parallel1:
                editParm(5);
                break;
            case R.id.lambertset_parallel2:
                editParm(6);
                break;
            case R.id.lambertset_utmc:
                editParm(7);
                break;
            default:
                break;
        }
    }

    private void editParm(int pos) {

        String strT = getString(R.string.lambertset_originlat);
        int REQUEST = REQUEST_TO_ZLAT;
        double value = mLambertSetInfo.ZLAT;
        switch (pos) {
            default:
            case 0:
                intent = EditNumberActivity.createIntentDLimt(context
                        , strT + getString(R.string.parmsetting), value, "", -90, 90);
                break;
            case 1:
                strT = getString(R.string.lambertset_originlon);
                value = mLambertSetInfo.ZLON;
                REQUEST = REQUEST_TO_ZLON;
                intent = EditNumberActivity.createIntentDLimt(context
                        , strT + getString(R.string.parmsetting), value, "", -180, 180);
                break;
            case 2:
                strT = getString(R.string.lambertset_scale);
                value = mLambertSetInfo.TY;
                REQUEST = REQUEST_TO_TY;
                intent = EditNumberActivity.createIntentD(context
                        , strT + getString(R.string.parmsetting), value, "");
                break;
            case 3:
                strT = getString(R.string.lambertset_dxpc);
                value = mLambertSetInfo.EW;
                REQUEST = REQUEST_TO_EW;
                intent = EditNumberActivity.createIntent(context
                        , strT + getString(R.string.parmsetting), (int) value, "");
                break;
            case 4:
                strT = getString(R.string.lambertset_nbpc);
                value = mLambertSetInfo.SN;
                REQUEST = REQUEST_TO_SN;
                intent = EditNumberActivity.createIntent(context
                        , strT + getString(R.string.parmsetting), (int) value, "");
                break;
            case 5:
                strT = getString(R.string.lambertset_parallel1);
                value = mLambertSetInfo.parallel1;
                REQUEST = REQUEST_TO_P1;
                intent = EditNumberActivity.createIntentDLimt(context
                        , strT + getString(R.string.parmsetting), value, "", -90, 90);
                break;
            case 6:
                strT = getString(R.string.lambertset_parallel2);
                value = mLambertSetInfo.parallel2;
                REQUEST = REQUEST_TO_P2;
                intent = EditNumberActivity.createIntentDLimt(context
                        , strT + getString(R.string.parmsetting), value, "", -90, 90);
                break;
            case 7:
                strT = getString(R.string.lambertset_utmc);
                value = mLambertSetInfo.utmc;
                REQUEST = REQUEST_TO_UTMC;
                intent = EditNumberActivity.createIntentD(context
                        , strT + getString(R.string.parmsetting), value, "");
                break;
        }

        toActivity(intent, REQUEST);
    }

    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_ZLAT = 31;
    private final int REQUEST_TO_ZLON = 32;
    private final int REQUEST_TO_TY = 33;
    private final int REQUEST_TO_EW = 34;
    private final int REQUEST_TO_SN = 35;

    private final int REQUEST_TO_P1 = 36;
    private final int REQUEST_TO_P2 = 37;
    private final int REQUEST_TO_UTMC = 38;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_ZLAT:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.ZLAT = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_ZLON:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.ZLON = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_TY:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.TY = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_EW:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.EW = Integer.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_SN:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.SN = Integer.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_P1:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.parallel1 = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_P2:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.parallel2 = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_UTMC:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mLambertSetInfo.utmc = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            mLambertSetInfo.reset();
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
